﻿using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Surging.Core.Caching;
using Surging.Core.CPlatform.Cache;
using Surging.Core.CPlatform.Ioc;

namespace Application.UserServices.Swagger
{
    public class SwaggerService : ServiceBase, ISwaggerService
    {
        private ICacheProvider _cacheProvider;
        public static readonly string SwaggerKey = "TemplateServices_swaggerkey";
        public SwaggerService()
        {
            _cacheProvider = CacheContainer.GetService<ICacheProvider>("MemoryCache");
        }
        public async Task<dynamic> Get()
        {
            var jsonStr = _cacheProvider.Get<string>(SwaggerKey);
            if (string.IsNullOrEmpty(jsonStr))
            {
                jsonStr = File.ReadAllText(Directory.GetCurrentDirectory() + "/swagger/swagger.json");
                if (!string.IsNullOrEmpty(jsonStr))
                {
                    _cacheProvider.Add(SwaggerKey, jsonStr);
                }
            }
            return await Task.FromResult(JsonConvert.DeserializeObject(jsonStr ?? "{}"));
        }
    }
}
